<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * Mall中心控制
 */
namespace Addon\Mall\Controller;
use Addon\Mall\Controller\CommonController;

/**
 * Mall中心控制
 */
class OrderController extends CommonController {
    

	//创建订单
	public function Create(){
		
	
		$m_mall_order = M('mall_order');
		
		$order_type = I('param.order_type','','');
		
		if($order_type==1){
			
		//格式化订单
		$itemcount=I('param.itemCount','','');
		
		$m_mall_content = M('mall_content');
		
		//没有买东西直接跳回
		if(!$itemcount){$this->error('你没有买东西！');die;}
		
		$order['express_type'] = I('param.express_type'); //快递类型 
		$order['pay_type'] = I('param.pay_type'); //支付类型
		
		//自提的话不是微信支付或者储值支付 就设置为微信支付
		if($order['express_type']==1 AND $order['pay_type']==4){
			
			$order['pay_type'] =1;
		}
		
		//如果自提
		if($order['express_type']==1){
			
			$order['client_name'] = I('param.name');
			$order['tel'] = I('param.tel');
			
			if(!$order['client_name']){$this->error('姓名不能为空。');die;}
			if(!$order['tel']){$this->error('电话不能为空。');die;}
		
			
			$amap['id'] = I('param.address_node');
			
			$adata = $m_mall_content->where($amap)->find();
			
			$order['address'] = $adata['value'];
		}else{
			
			$address = $this->getAddress();
			
			$order['client_name'] = $address['name'];
			$order['tel'] = $address['tel'];
			$order['address'] = $address['province'].$address['city'].$address['county'].$address['address'];
		}
		
		//循环获得购物车内信息
		for($i=1;$i<=$itemcount;$i++){
			
			//获得商品ID
			$order_info[$i]['goods_id'] = $this->getGoodsID(I('param.item_options_'.$i)); //商品ID
			
			$goods_info = $this->getGoodsInfo($order_info[$i]['goods_id']); //商品信息
			
			$order_info[$i]['goods_num']  = I('param.item_quantity_'.$i); //商品数量
			$order_info[$i]['name']  = $goods_info['name']; //商品名称
			$order_info[$i]['goods_money']  = $goods_info['money']; //商品单额
			$order_info[$i]['money']  = $order_info[$i]['goods_num']*$goods_info['money']; //商品总额
			$order_info[$i]['goods_partner_money']  = $order_info[$i]['goods_num']*$goods_info['partner_money']; //获得佣金
			$order_info[$i]['goods_integral']  = $order_info[$i]['goods_num']*$goods_info['member_integral']; //获得积分
			$order_info[$i]['goods_time']  = $goods_info['time']; //商品快照
			$order_info[$i]['goods_img']  = $goods_info['img']; //商品快照
			$order_info[$i]['status']  = 1; //默认都是1
			
		}
		
		$order['name'] = get_mall_order_conf('MALL_ORDER_NAME'); //订单名称
		
		foreach($order_info as $oi=>$ok){
			
			$order['money'] = $order['money']+$order_info[$oi]['money'];
			$order['goods_money'] = $order['goods_money']+($order_info[$oi]['goods_money']*$order_info[$oi]['goods_num']);
			$order['goods_num'] = $order['goods_num']+$order_info[$oi]['goods_num'];
			$order['goods_partner_money'] = $order['goods_partner_money']+$order_info[$oi]['goods_partner_money'];
			$order['goods_integral'] = $order['goods_integral']+$order_info[$oi]['goods_integral'];
			
			
		}
		
		$order['shipping'] = I('param.shipping'); //邮费
		$order['money'] = $order['money']+$order['shipping'];
		$order['order_type'] = get_mall_order_conf('MALL_ORDER_TYPE_GOODS'); //订单类型
		//$order['pay_type'] = I('param.pay_type'); //支付订单类型
		$order['remark'] = I('param.remark'); //订单留言
		$order['time'] = time(); //下单时间
		$order['ip'] = get_client_ip(); //下单IP
		$order['uid'] = getUID(); //用户
		
		
		$order['partner_id'] = getPartnerID(); //推荐人ID
		
		
		$order['order_id'] = $this->getOrderCode('MALL_ORDER_TYPE_GOODS'); //订单类型
		$order['day'] = getToday(); //订单类型
		
		//测试数据
		
		//储值支付 TODO事务回滚
		if($order['pay_type']=='2'){
			
			//够付款则付款
			if(getUserMoney()>=$order['money']){
					
				
				$m_common_user = M('common_user');
				$m_mall_money = M('mall_money');
				
				//创建财务信息
				$mdata['uid'] = getUID();
				$mdata['type'] = get_mall_order_conf('MALL_MONEY_TYPE_2');
				$mdata['type_value'] = get_mall_order_conf('MALL_MONEY_TYPE_2_1');
				$mdata['begin_money'] = getUserMoney();
				$mdata['money'] = $order['money'];
				$mdata['end_money'] = getUserMoney()-$order['money'];
				$mdata['value'] = '订单号'.$order['order_id'].'储值支付';
				
				$mdata['time'] = time();
				$mdata['ip'] = get_client_ip();
				$mdata['status'] = 1;
				
				
				$m_mall_money->add($mdata);
				
				//减去金额
				$umap['id'] = getUID();
				
				$m_common_user->where($umap)->setDec('money',$order['money']); //减少金额

				$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_4'); //直接待发货
	
				//创建订单
				$m_mall_order->add($order);
			
				foreach($order_info as $ooi=>$ook){
					
					$order_info[$ooi]['order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['form_order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['uid'] = getUID(); //订单类型
				
					$m_mall_order->add($order_info[$ooi]);
				};
				
				//增加订单信息
				$m_mall_content = M('mall_content');
				
				$odata['name'] = $order['order_id'];
				$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
				$odata['value'] = get_mall_order_conf('MALL_ORDER_INFO_1');
				$odata['time'] = time();
				$odata['ip'] = get_client_ip();
				$odata['status'] = 1;
				
				$m_mall_content->add($odata);
				
				$order_id = $order['order_id'];
				//跳转
				$callback = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']."/Home/Order/Accomplish/Addon/Mall.html?order_id=$order_id";
		
				redirect("$callback", 0,'');
				
			//不够则跳转
			}else{
				
				$this->error('储值不够，请充值。');die;
				
			}
			
			
			
		}else if($order['pay_type']==4  AND $order['express_type'] != '1'){
			
			
			if(getPartnerID()==1){
				
				
				$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_4'); //官方店铺直接待发货
	
				//创建订单
				$m_mall_order->add($order);
			
				foreach($order_info as $ooi=>$ook){
					
					$order_info[$ooi]['order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['form_order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['uid'] = getUID(); //订单类型
				
					$m_mall_order->add($order_info[$ooi]);
				};
				
				//增加订单信息
				$m_mall_content = M('mall_content');
				
				$odata['uid'] =	getUID();
		
				$odata['name'] = $order['order_id'];
				$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
				$odata['value'] = get_mall_order_conf('MALL_ORDER_INFO_2');
				$odata['time'] = time();
				$odata['ip'] = get_client_ip();
				$odata['status'] = 1;
				
				$m_mall_content->add($odata);
				
				//跳转
				//$this->error('下单成功，正在跳转。',U('Home/Mall/Order/Addon/MallTemplateHome',array('type'=>2)));die;
			
				$order_id = $order['order_id'];
				//跳转
				$callback = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']."/Home/Order/NoMoney/Addon/Mall.html?order_id=$order_id";
		
				redirect("$callback", 0,'');
				
			}else{
				
				$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_3'); //有合伙人需要合伙人扣除储值
	
				//创建订单
				$m_mall_order->add($order);
			
				foreach($order_info as $ooi=>$ook){
					
					$order_info[$ooi]['order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['form_order_id'] = $order['order_id']; //订单类型
					$order_info[$ooi]['uid'] = getUID(); //订单类型
				
					$m_mall_order->add($order_info[$ooi]);
				};
				
				//增加订单信息
				$m_mall_content = M('mall_content');
				
				$odata['uid'] = getUID();
		
				$odata['name'] = $order['order_id'];
				$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
				$odata['value'] = get_mall_order_conf('MALL_ORDER_INFO_3');
				$odata['time'] = time();
				$odata['ip'] = get_client_ip();
				$odata['status'] = 1;
				
				$m_mall_content->add($odata);
				
				//跳转
				//获得授权登录
				//$this->error('下单成功，正在跳转。',U('Home/Mall/Order/Addon/MallTemplateHome',array('type'=>2)));die;
				
				$order_id = $order['order_id'];
				//跳转
				$callback = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']."/Home/Order/NoMoney/Addon/Mall.html?order_id=$order_id";
		
				redirect("$callback", 0,'');
				
				
			}
			
			
			
			
		}else if($order['pay_type']==1){
			
			
			$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_1'); //待支付
	
			//创建订单
			$m_mall_order->add($order);
		
			foreach($order_info as $ooi=>$ook){
				
				$order_info[$ooi]['order_id'] = $order['order_id']; //订单类型
				$order_info[$ooi]['form_order_id'] = $order['order_id']; //订单类型
				$order_info[$ooi]['uid'] = getUID(); //订单类型
			
				$m_mall_order->add($order_info[$ooi]);
			};
			
			//增加订单信息
			$m_mall_content = M('mall_content');
			
			$odata['uid'] = getUID();
		
			$odata['name'] = $order['order_id'];
			$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
			$odata['value'] = get_mall_order_conf('MALL_ORDER_INFO_4');
			$odata['time'] = time();
			$odata['ip'] = get_client_ip();
			$odata['status'] = 1;
			
			$m_mall_content->add($odata);
				
			$order_id = $order['order_id'];
			//获得授权登录
			$callback = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']."/Home/OAuth/Pay/Addon/WeiXin.html?order_id=$order_id";
		
			redirect("$callback", 0,'');
			
		}
		
		}else if($order_type==2){
			
			
			
			
			$order['name'] = '储值订单'; //邮费
			$order['money'] = I('param.money'); //邮费
			$order['order_id'] = $this->getOrderCode('MALL_ORDER_TYPE_STORED'); //订单类型
			$order['order_type'] = get_mall_order_conf('MALL_ORDER_TYPE_STORED'); //订单类型
			$order['pay_type'] = 1; //支付订单类型
			$order['time'] = time(); //下单时间
			$order['ip'] = get_client_ip(); //下单IP
			$order['uid'] = getUID(); //用户
			$order['day'] = getToday(); //用户
			$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_1'); //待支付
			
			//创建订单
			$m_mall_order->add($order);
		
			//增加订单信息
			$m_mall_content = M('mall_content');
			
			$odata['uid'] = getUID();
		
			$odata['name'] = $order['order_id'];
			$odata['type'] = get_mall_model_conf('MALL_ORDER_INFO');
			$odata['value'] = get_mall_order_conf('MALL_ORDER_INFO_4');
			$odata['time'] = time();
			$odata['ip'] = get_client_ip();
			$odata['status'] = 1;
			
			$m_mall_content->add($odata);
				
			$order_id = $order['order_id'];
			//获得授权登录
			$callback = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME']."/Home/OAuth/Pay/Addon/WeiXin.html?order_id=$order_id";
		
			redirect("$callback", 0,'');
			
		}
	
		
		
	}
	
	//获得订单号
	public function Accomplish(){
		
		$m_mall_order = M('mall_order');
		
		$order_id = I('param.order_id');
		
		$m_mall_order = M('mall_order');
		
		$map['order_id'] = $order_id;
		$map['form_order_id'] = 0;
		
		$data = $m_mall_order->where($map)->find();
		
		$cancel_url = get_mall_order_conf('MALL_PAY_CANCEL_URL');//取消支付
		$order_url = get_mall_order_conf('MALL_PAY_ORDER_URL');//订单详情
		
		
		$return_url = get_mall_order_conf('MALL_PAY_RETURN_ORDER_URL');//支付成功跳转
		
		
		$this->assign('data',$data);
		
		$this->assign('cancel_url',$cancel_url);
		$this->assign('order_url',$order_url);
		$this->assign('return_url',$return_url);
		
		$this->view();
		
	}
	
	//获得订单号
	public function NoMoney(){
		
		$m_mall_order = M('mall_order');
		
		$order_id = I('param.order_id');
		
		$m_mall_order = M('mall_order');
		
		$map['order_id'] = $order_id;
		$map['form_order_id'] = 0;
		
		$data = $m_mall_order->where($map)->find();
		
		$cancel_url = get_mall_order_conf('MALL_PAY_CANCEL_URL');//取消支付
		$order_url = get_mall_order_conf('MALL_PAY_ORDER_URL');//订单详情
		
		
		$return_url = get_mall_order_conf('MALL_PAY_RETURN_ORDER_URL');//支付成功跳转
		
		
		$this->assign('data',$data);
		
		$this->assign('cancel_url',$cancel_url);
		$this->assign('order_url',$order_url);
		$this->assign('return_url',$return_url);
		
		$this->view();
		
	}
	
	//获得订单号
	public function getOrderCode($type){
		
		$m_mall_order = M('mall_order');
		
		$map['order_type'] = get_mall_order_conf($type);
		$map['form_order_id'] = 0;
		$map['day'] = getToday();
		
		$num = $m_mall_order->where($map)->count();
	
		$num = sprintf('%05s',$num+1);
		
		
		$code = date('Ymd',time()).$map['order_type'].$num;
		
		
		return $code;
		
	}
	
	//获得商品ID
	public function getGoodsID($info){
		
		$info = explode(",",$info);
			
		$goods_info = $info[0];
		
		$goods_info = explode(": ",$goods_info);
		
		
		$goods_id = $goods_info[1];
		
		return $goods_id;
		
	}
	
	//获得商品信息
	public function getGoodsInfo($id){
		
		$m_mall_content = M('mall_content');
		
		$map['id'] = $id;
		
		$data = $m_mall_content->where($map)->find();
		
		
		return $data;
		
	}
	
	//获得默认地址
	public function getAddress(){
		
		$m_mall_content = M('mall_content');
		
		$map['uid'] = getUID();
		$map['form_id'] = 0;
		$map['type'] = get_mall_model_conf('MALL_USER_ADDRESS');
		$map['status'] = 1;
		$map['sort'] = 1;

	
		$data = $m_mall_content->where($map)->find();
	
	
		return $data;
		
		
	}
	
}